package com.lagou.service.impl;

import com.lagou.dao.CourseContentMapper;
import com.lagou.domain.Course;
import com.lagou.domain.CourseLesson;
import com.lagou.domain.CourseSection;
import com.lagou.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseContentServiceImpl implements CourseContentService {

    @Autowired
    CourseContentMapper courseContentMapper;

    /**
     * 根据课程ID 查询课程内容(章节与课时)
     *
     * @param courseId 课程id
     * @return
     */
    @Override
    public List<CourseSection> findSectionAndLessonByCourseId(Integer courseId) {

        return courseContentMapper.findSectionAndLessonByCourseId(courseId);
    }

    /**
     * 回显章节对应的课程信息
     *
     * @param courseId 课程id
     * @return
     */
    @Override
    public Course findCourseByCourseId(Integer courseId) {
        return courseContentMapper.findCourseByCourseId(courseId);
    }

    /**
     * 新建章节信息
     *
     * @param courseSection
     */
    @Override
    public void saveSection(CourseSection courseSection) {

        Date date = new Date();
        courseSection.setCreateTime(date);
        courseSection.setUpdateTime(date);
        courseContentMapper.saveSection(courseSection);
    }

    /**
     * 修改章节
     *
     * @param courseSection
     */
    @Override
    public void updateSection(CourseSection courseSection) {

        courseSection.setUpdateTime(new Date());
        courseContentMapper.updateSection(courseSection);
    }

    /**
     * 修改章节状态
     * 状态，0:隐藏；1：待更新；2：已发布
     *
     * @param id     章节id
     * @param status 章节状态
     */
    @Override
    public void updateSectionStatus(Integer id, Integer status) {

        CourseSection courseSection = new CourseSection();
        courseSection.setId(id);
        courseSection.setStatus(status);
        courseSection.setUpdateTime(new Date());
        courseContentMapper.updateSectionStatus(courseSection);
    }

    /**
     * 保存课时
     *
     * @param courseLesson
     */
    @Override
    public void saveLesson(CourseLesson courseLesson) {

        Date date = new Date();
        courseLesson.setCreateTime(date);
        courseLesson.setUpdateTime(date);
        courseContentMapper.saveLesson(courseLesson);
    }
}
